/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rcs.filesystem;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.rcs.folders.VirtualItem;
import org.rcs.servlets.utils.FactoryConfig;
import org.rcs.servlets.utils.FileUtils;
import org.rcs.servlets.utils.XMLConfig;
import org.rcs.servlets.utils.XMLFileName;

/**
 *
 * @author rubén
 */
@XMLFileName(value = "FileSystemFactory.xml")
@XStreamAlias(value = "FileItemsFactory")
public class FileItemsFactoryConfig extends FactoryConfig {

  @XStreamAlias(value = "VideoPaths")
  private String[] videoItemsPath;
  @XStreamAlias(value = "AudioPaths")
  private String[] audioItemsPath;
  @XStreamAlias(value = "PicturePaths")
  private String[] pictureItemsPath;

  public FileItemsFactoryConfig() {
    this(FileItemsFactoryConfig.class, "File System", true);
  }
  
  public FileItemsFactoryConfig(Class clazz, String name, boolean enabled) { 
    super(clazz, name, enabled);

    // Add default data
    videoItemsPath = new String[] { FileUtils.getDefaultVideoDir().getPath() } ;
    audioItemsPath = new String[] { FileUtils.getDefaultAudioDir().getPath() } ;
    pictureItemsPath = new String[] { FileUtils.getDefaultPictureDir().getPath() } ;
  }

  protected VirtualItem[] convertPaths(String[] paths) {
    VirtualItem[] result = new VirtualItem[paths.length];
    
    for (int i = 0; i < result.length; ++i) {
      File file = new File(paths[i]);
      if (file.exists() && file.isDirectory())
        result[i] = new FolderItem(file);
      else
        result[i] = new FileItem(file);
    }
    
    return result;
  }

  /**
   * @return the videoItems
   */
  public VirtualItem[] getVideoItems() {
    return convertPaths(videoItemsPath);
  }

  /**
   * @param videoItems the videoItems to set
   */
  public void setVideoItemsPath(String... videoItems) {
    this.videoItemsPath = videoItems;
  }

  /**
   * @return the audioItems
   */
  public VirtualItem[] getAudioItems() {
    return convertPaths(audioItemsPath);
  }

  /**
   * @param audioItems the audioItems to set
   */
  public void setAudioItemsPath(String... audioItemsPath) {
    this.audioItemsPath = audioItemsPath;
  }

  /**
   * @return the pictureItems
   */
  public VirtualItem[] getPictureItems() {
    return convertPaths(pictureItemsPath);
  }

  /**
   * @param pictureItems the pictureItems to set
   */
  public void setPictureItemsPath(String... pictureItemsPath) {
    this.pictureItemsPath = pictureItemsPath;
  }

  public static void main(String[] args) {
    try {
      FileItemsFactoryConfig conf = new FileItemsFactoryConfig();

      conf.write();

      conf = XMLConfig.read(FileItemsFactoryConfig.class);

      conf.setEnabled(true);
      conf.write();
    } catch (IOException ex) {
      Logger.getLogger(FileItemsFactory.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}
